package com.sky.controller.sweet;

import com.sky.dto.FeedDTO;
import com.sky.dto.NestDTO;
import com.sky.mapper.SweetMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SweetService;
import com.sky.vo.BannerVO;

import com.sky.vo.NestVO;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j

@RequestMapping("/sweet/home")
public class SweetController {

    @Autowired
    private SweetService sweetService;

    @PostMapping("/getBanner")
    public Result<Map<String, Object>> getBanner(@RequestBody String type) {
        List<BannerVO> banners = sweetService.getBanner(type);
        List<Map<String, Object>> reports = sweetService.getReports();
        List<Map<String, Object>> categroyList = sweetService.getCateGoryList(type);
        Map<String, Object> data = new HashMap<>();
        data.put("banner", banners);
        data.put("reports", reports);
        data.put("categoryList", categroyList);
        return Result.success(data, "success");
    }


    @PostMapping("/getArticle")
    public Result<PageResult> getArticleList(@RequestBody FeedDTO feedDTO) {
        log.info("pageNum: {}, pageSize: {}", feedDTO.getPage(), feedDTO.getPageSize());
        PageResult pageResult = sweetService.getArticleList(feedDTO);
        return Result.success(pageResult, "success");
    }


    @PostMapping("/setGoods")
    public Result<String> setGoods(@RequestBody Map<String, Object> requestBody) {
        Long userId = Long.parseLong(requestBody.get("userId").toString());
        Long articleId = Long.parseLong(requestBody.get("articleId").toString());
        Number type = (Number) requestBody.get("type");
        sweetService.setGoods(userId, articleId, type);
        return Result.success(null, "success");
    }


    @PostMapping("/getArticleDetail")
    public Result<Map<String, Object>> getArticleDetail(@RequestBody Map<String, Object> requestBody) {
        // 检查请求体是否有效且包含 articleId
        if (requestBody == null || !requestBody.containsKey("articleId")) {
            return Result.error("articleId is required");
        }

        // 获取 articleId
        Long articleId = Long.parseLong(requestBody.get("articleId").toString());

        // 初始化返回数据
        Map<String, Object> data = new HashMap<>();

        // 获取文章 banner 数据
        List<Map<String, Object>> articleBanner = sweetService.getArticleBanner(articleId);

        // 获取文章详情数据
        List<Map<String, Object>> articleDetail = sweetService.getArticleDetail(articleId);

        // 如果没有找到 articleDetail 数据，返回错误信息
        if (articleDetail == null || articleDetail.isEmpty()) {
            return Result.error("Article details not found");
        }

        // 将数据放入 Map 中，确保有 title 和 content
        Map<String, Object> firstArticleDetail = articleDetail.get(0);
        String title = (String) firstArticleDetail.get("title");
        String content = (String) firstArticleDetail.get("content");
        data.put("banner", articleBanner);
        data.put("title", title);
        data.put("content", content);

        // 返回成功结果
        return Result.success(data, "success");
    }

    //获取小窝基础信息接口

    @PostMapping("/getNest")
    public Result<Map<String, Object>> getNest(@RequestBody Map<String, Object> requestBody) {
        if (requestBody == null || !requestBody.containsKey("userId")) {
            return Result.error("用户id不能为空");
        }
        Long userId = Long.parseLong(requestBody.get("userId").toString());
        // 初始化返回数据
        Map<String, Object> data = new HashMap<>();
        List<Map<String, Object>> nestInfoList = sweetService.getNest(userId);
        data.put("nestInfoList", nestInfoList);
        return Result.success(data, "success");
    }


    //更改小窝信息
    @PostMapping("/updateNest")
    public Result<NestVO> updateNestInfo(@RequestBody NestDTO nestDTO) {
        if (nestDTO.getNestId() == null) {
            return Result.error("小窝id不能为空");
        }

        int rowsAffected = sweetService.updateNestById(nestDTO);
        if (rowsAffected > 0) {

            // 返回成功响应并携带更新后的信息
            return Result.success(null, "更新成功");
        } else {
            // 如果没有更新的记录，返回错误信息
            return Result.error("更新失败");
        }

    }

    @PostMapping("/addNest")
    public Result<NestVO> addNest(@RequestBody NestDTO nestDTO) {
        if (nestDTO.getUser1Id() == null) {
            return Result.error("添加人id不能为空");
        }
        if (nestDTO.getUser2Id() == null) {
            return Result.error("被添加人id不能为空");
        }
        int haveNest = sweetService.haveNest(nestDTO);
        log.info("haveNest: {}", haveNest);
        if (haveNest > 0) {
            return Result.error("我焯,你出轨!!!");
        }
        int rowsAffected = sweetService.addNest(nestDTO);
        if (rowsAffected > 0) {
            return Result.success(null, "添加小窝成功");

        } else {
            // 如果没有更新的记录，返回错误信息
            return Result.error("添加小窝失败");
        }

    }

    @PostMapping("/setSelfTalk")
    public Result<String> setSelfTalk(@RequestBody Map<String, Object> requestBody) {
        Integer userId = Integer.parseInt(requestBody.get("userId").toString());
        String noteContent = (String) requestBody.get("noteContent");
        if (userId == null) {
            return Result.error("userId is required");
        }
        if (noteContent == null || noteContent.isEmpty()) {
            return Result.error("noteContent is required");
        }
        sweetService.setSelfTalk(userId, noteContent);
        return Result.success(null, "success");

    }

    //    获取碎碎念
    @PostMapping("/getSelfTalk")
    public Result<Map<String, Object>> getSelfTalk(@RequestBody Map<String, Object> requestBody) {
        Integer userId = Integer.parseInt(requestBody.get("userId").toString());
        Integer nestId = Integer.parseInt(requestBody.get("nestId").toString());
        Map<String, Object> data = new HashMap<>();
        List<Map<String, Object>> SelfTalkList = sweetService.getSelfTalklIST(userId, nestId);
        data.put("selfTalkList", SelfTalkList);
        return Result.success(data, "success");
    }

    @PostMapping("/setLikes")
    public Result<Map<String, Object>> setLikes(@RequestBody Map<String, Object> requestBody) {
        Integer userId = Integer.parseInt(requestBody.get("userId").toString());
        Integer noteId = Integer.parseInt(requestBody.get("noteId").toString());
        sweetService.setLikes(userId, noteId);
        return Result.success(null, "success");
    }

    @PostMapping("/category")
    public Result<Map<String, Object>> category(@RequestBody Map<String, Object> requestBody) {

        Integer nestId = Integer.parseInt(requestBody.get("nestId").toString());
        Map<String, Object> data = new HashMap<>();
        List<Map<String, Object>> categoryList = sweetService.getCategory(nestId);
        data.put("categoryList", categoryList);
        return Result.success(data, "success");
    }
}
